angular.module('transaction-detail',[])

.controller 'transactionDetailCtrl', ($http,userData,$scope,$ionicScrollDelegate,$ionicHistory,$ionicPopup,showToast) ->
    loadAlipayData=()->
        $http.get '/accounts',params:{action:'accountlist',stype:'alipay',userid:userData.getToken(),page:$scope.pageAlipay,pagesize:5}
        .success (resp) ->
            if resp.status == '0'
                $scope.allpageAlipay = resp.allpage
                $scope.tradelistAlipay=resp.tradelist
                console.log $scope.tradelistAlipay
                $scope.AlipayAllmoney = resp.allmoney
                $scope.totalMoneyAlipay = 0
                _.each $scope.tradelistAlipay,(item) ->
                    $scope.totalMoneyAlipay = $scope.totalMoneyAlipay + Number(item.totalAmt)
                    return $scope.totalMoneyAlipay
                console.log $scope.totalMoneyAlipay
            else
                showToast resp.msg
        .error ->
            showToast resp.msg

    loadWxData = () ->
        $http.get '/accounts',params:{action:'accountlist',stype:'wx',userid:userData.getToken(),page:$scope.pageWx,pagesize:5}
        .success (resp) ->
            if resp.status == '0'
                $scope.allpageWx = resp.allpage
                $scope.tradelistWx=resp.tradelist
                console.log $scope.tradelistWx
                $scope.WxAllmoney = resp.allmoney
                $scope.totalMoneyWx = 0
                _.each $scope.tradelistWx,(item) ->
                    $scope.totalMoneyWx = $scope.totalMoneyWx + Number(item.totalAmt)
                    return $scope.totalMoneyWx
                console.log $scope.totalMoneyWx
            else
                showToast resp.msg
        .error ->
            showToast resp.msg

    $scope.$on '$ionicView.enter',->
        $scope.pageAlipay = 1
        $scope.currentPageAlipay = 1
        $scope.pageWx = 1
        $scope.currentPageWx = 1
        loadAlipayData()
        loadWxData()
    _.extend $scope,
        goBack: ->
            $ionicHistory.goBack()
        changeSource:->
            if $scope.source=='web'
                $scope.source='pc'
            else
                $scope.source='web'
        getMoreAlipay:->
            console.log $scope.totalMoneyAlipay
            if $scope.currentPageAlipay < $scope.allpageAlipay
                $scope.currentPageAlipay = $scope.pageAlipay + $scope.currentPageAlipay
                console.log $scope.currentPageAlipay
                $http.get '/accounts',params:{action:'accountlist',stype:'alipay',userid:userData.getToken(),page:$scope.currentPageAlipay,pagesize:5}
                .success (resp) ->
                    if resp.status == '0'
                        $scope.tradelistAlipayMore=resp.tradelist
                        console.log $scope.tradelistAlipayMore
                        $scope.tradelistAlipay = _.union($scope.tradelistAlipay,$scope.tradelistAlipayMore)
                        $scope.totalMoneyAlipayMore = 0
                        _.each $scope.tradelistAlipayMore,(item) ->
                            $scope.totalMoneyAlipayMore = $scope.totalMoneyAlipayMore + Number(item.totalAmt)
                            return $scope.totalMoneyAlipayMore
                        console.log $scope.totalMoneyAlipayMore
                        $scope.totalMoneyAlipay = $scope.totalMoneyAlipay + $scope.totalMoneyAlipayMore
                        console.log $scope.totalMoneyAlipay
                    else
                        showToast resp.msg
                .error ->
                    showToast resp.msg
            else
                showToast '没有更多数据了'

        getMoreWx: ->
            console.log $scope.totalMoneyWx
            if $scope.currentPageWx < $scope.allpageWx
                $scope.currentPageWx = $scope.pageWx + $scope.currentPageWx
                console.log $scope.currentPageWx
                $http.get '/accounts',params:{action:'accountlist',stype:'wx',userid:userData.getToken(),page:$scope.currentPageWx,pagesize:5}
                .success (resp) ->
                    if resp.status == '0'
                        $scope.tradelistWxMore=resp.tradelist
                        console.log $scope.tradelistWxMore
                        $scope.tradelistWx = _.union($scope.tradelistWx,$scope.tradelistWxMore)
                        $scope.totalMoneyWxMore = 0
                        _.each $scope.tradelistWxMore,(item) ->
                            $scope.totalMoneyWxMore = $scope.totalMoneyWxMore + Number(item.totalAmt)
                            return $scope.totalMoneyWxMore
                        console.log $scope.totalMoneyWxMore
                        $scope.totalMoneyWx = $scope.totalMoneyWx + $scope.totalMoneyWxMore
                        console.log $scope.totalMoneyWx
                    else
                        showToast resp.msg
                .error ->
                    showToast resp.msg
            else
                showToast '没有更多数据了'
